/*
 * comlan
 */

package com.promfy.bughunter.views;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.promfy.bughunter.BugHunterActivity;
import com.promfy.bughunter.R;
import com.promfy.bughunter.db.DBService;
import com.promfy.bughunter.domain.Score;

public class GameResultsView extends LinearLayout implements OnClickListener{


	private LayoutInflater inflater;

	private Context context;

	private View view;

	private ListView listView;

	private List<Score> scores = new ArrayList<Score>();
	
	private com.promfy.bughunter.views.GameResultsView.GameResultsAdapter adapter;

	private Button resume;

	private Button restart;

	private Button exit;

	
	public GameResultsView(Context context) {
		super(context);
		this.context = context;
	
		initView();
		initData();
	}

	public GameResultsView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	private void initView(){
		inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.game_results, this);
		listView = (ListView)view.findViewById(R.id.listview_game_results);
		resume = (Button)view.findViewById(R.id.btn1_game_results);
		restart = (Button)view.findViewById(R.id.btn2_game_results);
		exit = (Button)view.findViewById(R.id.btn3_game_results);
		
		resume.setOnClickListener(this);
		restart.setOnClickListener(this);
		exit.setOnClickListener(this);
		
		adapter = new GameResultsAdapter(context, 0, R.layout.game_results_listview, scores);
		listView.setAdapter(adapter);
		listView.setCacheColorHint(0);
	}
	
	private void initData() {
		DBService dbService = new DBService(context);
		try {
			List<Score> sl = dbService.getAll();
			if(sl!=null && sl.size()!=0){
				scores.clear();
				scores.addAll(sl);
				adapter.notifyDataSetChanged();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn1_game_results:
			//hide this view, resume game
			setVisibility(INVISIBLE);
			((BugHunterActivity)context).onState(BugHunterActivity.STATUS_GAME_MODE);
			break;
		case R.id.btn2_game_results:
			//hide this view, start a new GamePlayingView
			GamePlayingView.setPause(false);
			setVisibility(INVISIBLE);
			// start a new game 
			((BugHunterActivity)context).onState(BugHunterActivity.STATUS_GAME_PLAYING);
			break;
		case R.id.btn3_game_results:
			// finish activity
			((Activity)context).finish();
			break;
		default:
			break;
		}
	}

	
	private class GameResultsAdapter extends ArrayAdapter {

		private LayoutInflater inflater;
		private ArrayList<Score> scoreList;

		private Bitmap collapseIcon;
		private Bitmap expandIcon;

		public GameResultsAdapter(Context context, int resource, int textViewResourceId, List objects) {
			super(context, resource, textViewResourceId, objects);
			inflater = LayoutInflater.from(context);
			scoreList = (ArrayList<Score>) objects;

		}

		@Override
		public Object getItem(int position) {
			return scoreList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.game_results_listview, null);

				holder = new ViewHolder();
				holder.date = (TextView) convertView.findViewById(R.id.date_game_results_listview);
				holder.score = (TextView) convertView.findViewById(R.id.score_game_results_listview);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			try {

				Score score = (Score) getItem(position);
				holder.date.setText(score.getDate());
				holder.score.setText(score.getScore());
				
			} catch (Exception e) {
				e.printStackTrace();
			}

			return convertView;
		}

		class ViewHolder {
			TextView date;
			TextView score;
		}

		@Override
		public int getCount() {
			return scoreList.size();
		}

	}


}
